package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import view.MainFrame;
import view.PatientTab;
import view.SearchTab;
import model.Patient;
import model.PorterStemmer;
import model.WebCrawler;
import model.XLSReader;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Controller class, used to connect thingies to avoid dependencies
 * 
 * @author Audun
 * 
 */

public class Controller implements ActionListener, ListSelectionListener {
	private WebCrawler crawler;
	private MainFrame gui;
	private XLSReader XLSReader;

	private JButton searchButton, diagnosisSearchButton;
	private JTextField searchField;
	private JList patientList;
	private JMenuItem about, exitMenuItem;
	private PatientTab patientTab;
	private SearchTab searchTab;
	private JList linkList;
	private Patient currentlySelectedPatient;

	public Controller(MainFrame gui, XLSReader XLSReader) {
		this.XLSReader = XLSReader;
		this.gui = gui;
		patientTab = gui.getPatientTab();
		searchTab = gui.getSearchTab();
		crawler = searchTab.getEngine();

		patientList = patientTab.getList();
		patientList.addListSelectionListener(this);

		linkList = searchTab.getResultList();
		linkList.addListSelectionListener(this);

		searchButton = searchTab.getSearchButton();
		searchButton.addActionListener(this);
		
		exitMenuItem = gui.getexitMenuItem();
		exitMenuItem.addActionListener(this);

		diagnosisSearchButton = patientTab.getDiagnosisSearchButton();
		diagnosisSearchButton.addActionListener(this);

		searchField = searchTab.getSearchField();
		searchField.addActionListener(this);

		about = gui.getAbout();
		about.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == searchButton) {
			crawler.handleSearchRequest();
		} else if (e.getSource() == searchField) {
			crawler.handleSearchRequest();
		} else if (e.getSource() == about) {
			gui.aboutMessage();
		} else if (e.getSource() == exitMenuItem) {
			System.exit(0);
		} else if (e.getSource() == diagnosisSearchButton) {
			int stemOrNot = patientTab.stemOrNot();
			if (stemOrNot == 0) {
				searchTab.getSearchField().setText(
						currentlySelectedPatient.getsEADiagnosis());
				gui.getTabbedPane().setSelectedComponent(searchTab);
			} else if (stemOrNot == 1) {
				String diagnosisString = currentlySelectedPatient
						.getsEADiagnosis();
				StringTokenizer st = new StringTokenizer(diagnosisString);
				String stemmedString = "";
				PorterStemmer ps = new PorterStemmer();
				while (st.hasMoreTokens()) {
					stemmedString = stemmedString + " "
							+ ps.stem(st.nextToken());

				}

				searchTab.getSearchField().setText(stemmedString);
				gui.getTabbedPane().setSelectedComponent(searchTab);
			}
		}
	}

	/**
	 * Displays patient data in the text area
	 * 
	 * @param patientID
	 */
	public void showPatientData(int patientID) {
		currentlySelectedPatient = new Patient(
				XLSReader.convertCellArraytoStringArray(XLSReader
						.getRowArray(patientID)));
		patientTab.setText(currentlySelectedPatient.toString());

	}

	/**
	 * Opens up an URL in the default browser
	 * 
	 * @param url
	 */
	public void openLinkInBrowser(String url) {
		java.awt.Desktop browserDesktop = java.awt.Desktop.getDesktop();
		java.net.URI website;
		try {
			website = new java.net.URI(url);
			browserDesktop.browse(website);
		} catch (URISyntaxException e) {
			popupMsg("Wrong URL");
		} catch (IOException e) {
			popupMsg("Could not launch browser");
		}

	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getSource().equals(patientList)) {
			JList tempList = (JList) e.getSource();
			showPatientData(tempList.getSelectedIndex());
		} else if (e.getSource().equals(linkList)) {
			JList tempList = (JList) e.getSource();
			openLinkInBrowser((String) tempList.getModel().getElementAt(
					tempList.getSelectedIndex()));
		}
	}

	/**
	 * Displays a popup message
	 * 
	 * @param message
	 */
	public void popupMsg(String message) {
		JOptionPane popupPane = new JOptionPane();
		popupPane.showMessageDialog(null, message);
	}
}
